﻿using CodeAngel.Domain;
using CodeAngel.Domain.Aggregates;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace CodeAngel.UI
{
    class SelectDomainViewModel : ViewModel
    {
        #region Constructors

        public SelectDomainViewModel()
        {
            this.LoadAvailableDomainsAsync();
        }

        #endregion

        #region SelectedDomain

        /// <summary>
        /// Gets or sets the SelectedDomain.
        /// </summary>
        public Domain.Domain SelectedDomain
        {
            get { return this._SelectedDomain; }
            set
            {
                
                if (value != this._SelectedDomain)
                {
                    this._SelectedDomain = value;
                    this.RaisePropertyChanged("SelectedDomain");
                }
            }
        }
        private Domain.Domain _SelectedDomain;


        #region DomainSelected

        /// <summary>
        /// Provides notification that the Domain has Selected.
        /// </summary>
        public event EventHandler DomainSelected;

        /// <summary>
        /// Raises the DomainSelected event.
        /// </summary>
        protected void RaiseDomainSelectedEvent()
        {
            if (this.DomainSelected != null) this.DomainSelected(this, EventArgs.Empty);
        }

        #endregion

        #endregion

        #region AvailableDomains

        /// <summary>
        /// Gets or sets the AvailableDomains.
        /// </summary>
        public Domain.DomainCollection AvailableDomains
        {
            get { return this._availableDomains; }
            set
            {
                if (value != this._availableDomains)
                {
                    this._availableDomains = value;
                    this.RaisePropertyChanged("AvailableDomains");
                }
            }
        }
        private Domain.DomainCollection _availableDomains;

        /// <summary>
        /// Loads all available Domains and makes them available through the AvailableDomains property.
        /// </summary>
        private async void LoadAvailableDomainsAsync()
        {
            var DomainsAggregate = new Domain.Aggregates.DomainListAggregate();
            await DomainsAggregate.LoadAllAsync();
            this.AvailableDomains = DomainsAggregate.Root;
        }

        #endregion

        #region OpenDomain

        public void OpenDomain(Domain.Domain thisDomain)
        {
            this.SelectedDomain = thisDomain;
            this.RaiseDomainSelectedEvent();
        }


        /// <summary>
        /// Delegate command that can execute OpenDomain.
        /// </summary>
        public DelegateCommand<Domain.Domain> OpenDomainCommand
        {
            get
            {
                if (this._OpenDomainCommand == null) this._OpenDomainCommand = new DelegateCommand<Domain.Domain>(OpenDomain);
                return this._OpenDomainCommand;
            }
        }
        private DelegateCommand<Domain.Domain> _OpenDomainCommand;

        #endregion
    }
}
